// Copyright 2019 Proyectos y Sistemas de Mantenimiento SL (eProsima).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ------------------------------------------------------------------
// Modification history:
// feature: Unified Transport Abstraction Layer Unified Transport Abstraction Layer
// ------------------------------------------------------------------

#ifndef SRC_CPP_RTPS_TRANSPORT_TCP_RTCPHEADER_H_
#define SRC_CPP_RTPS_TRANSPORT_TCP_RTCPHEADER_H_

#include <edds/rtps/common/Types.h>

#include <ecdr/ECdr.h>
#include <ecdr/Cdr.h>

#include <ecdr/exceptions/BadParamException.h>
#if defined(_WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#endif

#include <cstring>
using namespace evbs;

namespace vbs {
namespace transport {
#define TCPHEADER_SIZE 8U

// TCP Header structs and enums.
struct TCPHeader {
    char rtcp[4];
    uint32_t length;

    TCPHeader() : length(TCPHEADER_SIZE) {
        // There isn't a explicit constructor because VS2013 doesn't support it.
        rtcp[0] = 'R';
        rtcp[1] = 'T';
        rtcp[2] = 'C';
        rtcp[3] = 'P';
    }

    TCPHeader(uint32_t _length) : TCPHeader() { length = _length; }

    void set_length(const uint32_t _length) { length = _length; }

    const ertps::rtps::octet* address() const { return reinterpret_cast<const ertps::rtps::octet*>(this); }

    ertps::rtps::octet* address() { return (ertps::rtps::octet*)this; }

    /*!
     * @brief This function returns the maximum serialized size of an object
     * depending on the buffer alignment.
     * @return Maximum serialized size.
     */
    static inline size_t size() { return TCPHEADER_SIZE; }
};

// inline std::ostream& operator<<(std::ostream& output, const TCPTransactionId& t) {
//     bool printed = false;  // Don't skip cases like 99 0 34
//     for (int i = 2; i >= 0; --i) {
//         if (printed || (i == 0) || (t.ints[i] > 0U)) {
//             output << t.ints[i];
//             printed = true;
//         }
//     }
//     return output;
// }

}  // namespace transport
}  // namespace vbs

#endif  // SRC_CPP_RTPS_TRANSPORT_TCP_RTCPHEADER_H_
